﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VideoCache.Server.MVCWeb.Attributes;
using VideoCache.Server.BLL;
using VideoCache.Server.MVCWeb.Areas.Admin.Models;

namespace VideoCache.Server.MVCWeb.Areas.Admin.Controllers
{
    [ActionAuthorize(AuthorizeType.Administrator)]
    public class DictionaryController : Controller
    {
        //
        // GET: /Admin/Dictionary/
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult List()
        {
            DictionaryListModel model = new DictionaryListModel();

            var list = DictionaryBLL.FindAll(null, null);
            foreach (var item in list)
            {
                model.rows.Add(new DictionaryListItemModel(item));
            }

            return Json(model, JsonRequestBehavior.AllowGet);
        }

        public ActionResult ListTree()
        {
            var list = DictionaryBLL.FindAll(null, null);

            DictionaryTreeModel model = new DictionaryTreeModel();
            model.id = 0;
            model.text = "所有字典";

            this.GetChild(model, list);

            return Json(new DictionaryTreeModel[] { model }, JsonRequestBehavior.AllowGet);
        }

        private void GetChild(DictionaryTreeModel model, List<Model.DictionaryInfo> list)
        {
            foreach (var item in list.FindAll(p => p.ParentId == model.id))
            {
                DictionaryTreeModel treeModel = new DictionaryTreeModel(item);
                model.children.Add(treeModel);

                this.GetChild(treeModel, list);
            }
        }
    }
}